/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.file;

import it.francescogabbrielli.android.solidalapp.Constants;
import it.francescogabbrielli.android.solidalapp.DataEvent;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.StatusEvent;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.profileendpoint.model.Profile;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * 
 */
public class RequestFileFragment extends FileFragment implements LoaderCallbacks<Profile> {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View ret = inflater.inflate(R.layout.fragment_file_request, container, false);
		enableButtons(false);
		return ret;
	}
	
	@Override
	public void onStatusUpdate(StatusEvent event) {
	
	}
	
	@Override
	public void onDataChanged(DataEvent event) {
		
		Profile profile = (Profile) app.getData(DataKey.FILE_RESPONSE_PROFILE);
		Response response = (Response) app.getData(DataKey.FILE_RESPONSE);
		
		if (response==null)
			return;
		
		switch (event.getKey()) {
		
		case FILE_RESPONSE:
			
			if (response.getCompleted()) {
				TextView tv = (TextView) getView().findViewById(R.id.file_send_feedback_text);
				tv.setText(R.string.file_closed);
			}
			
			Bundle filters = new Bundle();
			filters.putLong("id", response.getSenderId());
			LoaderManager lm = getActivity().getSupportLoaderManager();
			if (event.isTriggered())
				lm.restartLoader(Constants.LOADER_FILE_RESPONSE_PROFILE, filters, this);
			else
				lm.initLoader(Constants.LOADER_FILE_RESPONSE_PROFILE, filters, this);
			break;
			
		case FILE_RESPONSE_PROFILE:
			updateProfile(profile, response.getMessage(), response.getTimestamp());
			break;

		default:
			break;
		}
		
		enableButtons(profile!=null && !response.getCompleted());
	}
	
	@Override
	public Loader<Profile> onCreateLoader(int id, final Bundle data) {
		Response res = (Response) app.getData(DataKey.FILE_RESPONSE);
		return newLoader(res!=null ? res.getRegistrationId() : null)
				.setFilters(data);
	}

	@Override
	public void onLoadFinished(Loader<Profile> loader, Profile result) {
		app.setData(DataKey.FILE_RESPONSE_PROFILE, result);
	}

	@Override
	public void onLoaderReset(Loader<Profile> loader) {
		
	}
	
	private void enableButtons(boolean enabled) {
		View v = getView();
		if (v!=null) {
			Button b = (Button) v.findViewById(R.id.file_button_ok);
			b.setEnabled(enabled);
			b = (Button) v.findViewById(R.id.file_button_ko);
			b.setEnabled(enabled);
		}
	}
	
}
